package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.PageVo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.common.R2;
import com.twentytwok.hospitalsystem22K.mapper.CommentMapper;
import com.twentytwok.hospitalsystem22K.pojo.Comment;
import com.twentytwok.hospitalsystem22K.pojo.DoctorUser;
import com.twentytwok.hospitalsystem22K.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public PageVo<Comment> getPageData(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("page")+""),Integer.parseInt(params.get("limit")+""));

        List<Comment> comments = commentMapper.selectByParams(params);

        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);

        PageVo<Comment> commentPageVo = new PageVo<>();

        commentPageVo.setCode(0);
        commentPageVo.setMsg("查询用户分页数据");
        commentPageVo.setCount(commentPageInfo.getTotal());
        commentPageVo.setData(commentPageInfo.getList());
        return commentPageVo;
    }

    @Override
    public R2 update(Comment comment) {
        int result = commentMapper.update(comment);

        if (result == 1){
            return R2.ok(200, "添加成功");
        } else {
            return R2.error(400, "添加失败");
        }
    }

    @Override
    public R2 delete(Integer id) {
        int result = commentMapper.delete(id);

        if (result == 1){
            return R2.ok(200, "删除成功");
        } else {
            return R2.error(400, "删除失败");
        }
    }

    @Override
    public R2 add(Comment comment) {
        int result  = commentMapper.add(comment);

        if (result == 1){
            return R2.ok(200, "添加成功");
        } else {
            return R2.error(400, "添加失败");
        }
    }
}
